﻿using Microsoft.EntityFrameworkCore.Infrastructure;
using SheepStorage.Domain.DTO.Output;
using SheepStorage.Domain.Entity;
using SheepStorage.Repository.Interface;
using SheepStorage.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SheepStorage.Service.DataImp
{
    /// <summary>
    /// 羊场首页统计仓储
    /// </summary>
    public class statisticsService:IstatisticsService
    {

        private readonly IBaseRepository<PromptModel> prompt;
        private readonly IBaseRepository<SheepRecord> record;
        public statisticsService(IBaseRepository<PromptModel> prompt, IBaseRepository<SheepRecord> record)
        {
            this.prompt = prompt;
            this.record = record;
        }
        /// <summary>
        /// 生产提示
        /// </summary>
        /// <returns></returns>
       
        public ProductionpromptOutput CountWaitingbreed()
        {

            var Productionprompt = new ProductionpromptOutput();
            //待配种
             var Waitingbreed = prompt.GetAll().Where(s => s.State == "1").ToList();
            //首次妊娠
            var Firstpregnancy = prompt.GetAll().Where(s => s.State == "2").ToList();
            //待分娩
             var Awaitingdelivery = prompt.GetAll().Where(s => s.State == "3").ToList();

            Productionprompt.CountWaitingbreed = Waitingbreed.Count();
            Productionprompt.CountFirstpregnancy = Firstpregnancy.Count();
            Productionprompt.CountAwaitingdelivery = Awaitingdelivery.Count();
            return Productionprompt;

        }

        /// <summary>
        /// 羊场实时存栏
        /// </summary>
        /// <returns></returns>
        public SheepinventoryOutput CountSheepinventory()
        {
            var Sheepinventory = new SheepinventoryOutput();
            //总存栏
            Sheepinventory.SheepCount = record.GetAll().Where(s => s.SheepRecordPresentState == 1 && s.IsDel == false).Count();
            //种公存栏
            Sheepinventory.repositoryCount = record.GetAll().Where(s => s.SheepRecordType == 1&& s.SheepRecordPresentState == 1 && s.IsDel == false).Count();
            //种母存栏
            Sheepinventory.ParentCount = record.GetAll().Where(s => s.SheepRecordType == 2 && s.SheepRecordPresentState == 1 && s.IsDel == false).Count();
            //后裔存栏
            Sheepinventory.DescendantCount = record.GetAll().Where(s => s.SheepRecordType == 3 && s.SheepRecordPresentState == 1 && s.IsDel == false).Count();
            return Sheepinventory;
        }


        /// <summary>
        /// 羊场30日存栏
        /// </summary>
        /// <returns></returns>
        public monthCountOutput monthCountSheepinventory()
        {
            var monthCount = new monthCountOutput();
            monthCount.DailyCounts = new Dictionary<DateTime, int>();
            // 获取当前日期
            DateTime currentDate = DateTime.Today;
            // 循环遍历过去30天
            for (int i = 0; i < 30; i++)
            {
                // 计算当前日期向前推i天的日期
                DateTime dateToQuery = currentDate.AddDays(-i).Date;
                // 查询符合条件的记录数
                int count = record.GetAll()
                                  .Count(s => s.SheepRecordPresentState == 1
                                               && s.IsDel == false
                                               && s.AddTime.HasValue && s.AddTime.Value.Date == dateToQuery);
                // 将结果存入输出对象中
                monthCount.DailyCounts[dateToQuery.Date] = count;
            }
            return monthCount;
        }
        ///// <summary>
        ///// 存栏结构
        ///// </summary>
        ///// <param name="SheepRecordVariety"></param>
        ///// <returns></returns>
        //public StoragestructureOutput GetStoragestructure(int SheepRecordVariety)
        //{
        //    var StoragesOutput = new StoragestructureOutput();

        //    if(SheepRecordVariety==1)
        //    {
        //        StoragesOutput.WaitingbreedCount = record.GetAll().Where(s => s.SheepRecordVariety ==1 &&s.SheepRecordBreedState >6&&s.IsDel==false&&s.SheepRecordPresentState==0).Count();
        //        StoragesOutput.PendingpregnancyCount = prompt.GetAll().Where(s => s.State == "2").Count();
        //    }
        //}
    }
}
